package delay;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 普通消费者
 *
 * @author Administrator
 * @date 2022/03/31 14:08
 **/
@Slf4j
public class DelayConsumer {
    public static void main(String[] args)throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("Delay_Consumer");
        consumer.setNamesrvAddr("rocketmq1.cn:9876;rocketmq2.cn:9876");
        // topic tag
        consumer.subscribe("Delay-Msg","order");
        // 回调函数
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {


                for (MessageExt msg : msgs) {
                    log.info("推送过来的消息:{} 消息的发送时间:{}, 消息的消费时间:{} ,相差时间:{}"
                            , StrUtil.str(msg.getBody(),"utf-8"),msg.getStoreTimestamp(),System.currentTimeMillis(),(System.currentTimeMillis()-msg.getStoreTimestamp()));
                }

                // 返回消费成功
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        // 开始消费
        consumer.start();
        log.info("延时消费开始");
    }
}
